/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.service;

import java.util.List;
import java.util.Set;

import com.javaeye.order.dao.CustomerDAO;
import com.javaeye.order.dao.ProductDAO;
import com.javaeye.delivery.dao.CheckOrderDAO;
import com.javaeye.delivery.dao.DeliveryOrderDAO;
import com.javaeye.delivery.dao.OrderDAO;
import com.javaeye.delivery.dao.ReasonDAO;
import com.javaeye.order.dto.Customer;
import com.javaeye.order.dto.Product;
import com.javaeye.delivery.dto.CustomerOrder;
import com.javaeye.delivery.dto.DeliveryOrder;
import com.javaeye.delivery.dto.DeliveryOrderDetail;
import com.javaeye.delivery.dto.OrderCheck;
import com.javaeye.delivery.dto.OrderDetail;
import com.javaeye.delivery.dto.Reason;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.util.NumberUtils;

public class CheckOrderService implements ICheckOrderService {
	
	private CheckOrderDAO dao;
	
	private OrderDAO orderDao;
	
	private ReasonDAO reasonDao;
	
	private DeliveryOrderDAO deliveryOrderDao;
	
	private CustomerDAO customerDao;
	
	private ProductDAO productDao;
	
	public void setDao(CheckOrderDAO dao) {
		this.dao = dao;
	}
	
	public void setOrderDao(OrderDAO orderDao) {
		this.orderDao = orderDao;
	}

	public void setReasonDao(ReasonDAO reasonDao) {
		this.reasonDao = reasonDao;
	}

	public void setDeliveryOrderDao(DeliveryOrderDAO deliveryOrderDao) {
		this.deliveryOrderDao = deliveryOrderDao;
	}

	public void setCustomerDao(CustomerDAO customerDao) {
		this.customerDao = customerDao;
	}
	
	public void setProductDao(ProductDAO productDao) {
		this.productDao = productDao;
	}

	public void saveCheckResult(OrderCheck checkResult) {
		CustomerOrder order =  orderDao.getOrderBaseInfo(checkResult.getOrderId());
		
		double hours = DateUtils.getDateOffsetHour(order.getUpdateDate(), DateUtils.now());
		if (order.getStatus() == IOrderService.ORDER_STATES_IN_STORE && hours > Reason.LATER_HOUR) {
			Reason reason = new Reason(Reason.REASON_CATEGORY_CHECK, 
					order.getId(),
					"检验安排在仓库入库完成" + Reason.LATER_HOUR + "个小时之后");
			reasonDao.saveReason(reason);
		}

		//保存检验信息
		dao.saveCheckResult(checkResult);
		if (Integer.parseInt(checkResult.getResult()) == 2) {
			order.setStatus(IOrderService.ORDER_STATES_CHECKED);
			order.setUpdateDate(DateUtils.now());
			orderDao.saveOrder(order);
			
			//记录一次不合格
			Reason reason = new Reason(Reason.REASON_CATEGORY_MADE, 
					order.getId(),
					"检验不合格");
			reasonDao.saveReason(reason);
		} else {
			order.setStatus(IOrderService.ORDER_STATES_CREATED_DELIVERY_ORDER);
			order.setUpdateDate(DateUtils.now());
			orderDao.saveOrder(order);
			//创建成品交运单
			DeliveryOrder dOrder = initDeliveryOrderInfo(order);
			deliveryOrderDao.save(dOrder);
		}
	}
	
	public List<OrderCheck> getCheckResult(String orderId) {
		List<OrderCheck> checkResults = dao.getCheckResult(orderId);
		for (OrderCheck c : checkResults) {
			c.setResult(ListUtil.checkResultName(c.getResult()));
		}
		return checkResults;
	}
	
	private DeliveryOrder initDeliveryOrderInfo(CustomerOrder order) {
		DeliveryOrder dOrder = new DeliveryOrder();
		dOrder.setId(order.getId());
		dOrder.setPurchaseOrderId(order.getPurchaseOrderId());
		dOrder.setLogisticsType(order.getLogisticsType());
		
		int customId = order.getCustomId();
		Customer customer = customerDao.getCustomer(customId);
		dOrder.setCustomId(customId);
		dOrder.setCustomName(customer.getName());
		dOrder.setReceivers(customer.getContact());
		dOrder.setAddress(customer.getAddress());
		dOrder.setTelephone(customer.getPhone());
		
		
		double totleAmount = 0;
		double totleWeight = 0;
		double totleVolume = 0;
		Set<OrderDetail> details = order.getOrderDetails();
		for (OrderDetail detail : details) {
			DeliveryOrderDetail dDetail = new DeliveryOrderDetail();
			int productId = detail.getProductId();
			Product product = productDao.getProduct(productId);
			dDetail.setProductId(productId);
			dDetail.setProductName(detail.getProductName());
			dDetail.setProductCategory(detail.getProductCategory());
			dDetail.setProductType(detail.getProductType());
			dDetail.setProductNo(detail.getProductNo());
			dDetail.setCustomNo(detail.getCustomNo());
			dDetail.setBatchNumber(detail.getBatchNumber());
			dDetail.setNumber(detail.getNumber());
			dDetail.setUnit(detail.getUnit());
			dDetail.setPrice(detail.getPrice());
			dDetail.setPackageNumber(detail.getPackageNumber());
			dDetail.setAmount(dDetail.getPrice() * dDetail.getNumber());
			totleAmount += dDetail.getAmount();
			totleWeight += product.getWeight() * dDetail.getPackageNumber();
			totleVolume += product.getVolume() * dDetail.getPackageNumber();
			dOrder.addDetail(dDetail);
		}
		dOrder.setTotleAmount(NumberUtils.formatDouble(totleAmount));
		dOrder.setTotleWeight(NumberUtils.formatDouble(totleWeight));
		dOrder.setTotleVolume(NumberUtils.formatDouble(totleVolume));
		
		return dOrder;
	}

}
